package sk.kainitram.fuel;

import java.util.ArrayList;
import java.util.List;

import com.kainitram.morm.*;

import sk.kainitram.fuel.dataAdapter.CarsAdapter;
import sk.kainitram.fuel.db.CarContract;
import sk.kainitram.fuel.model.*;
import sk.kainitram.listeners.SwipeDismissListViewTouchListener;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CarsActivity extends Fragment {

	private CarsAdapter carsAdapter;
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		setRetainInstance(false);
        View ret = inflater.inflate(R.layout.cars, container, false);
        
        initFakeData(ret);
        
        return ret;
    }
	
	
	private void initFakeData(View view)
	{
		ListView listView = (ListView)view.findViewById(R.id.lvCars);

		Session session = new SessionBase();
		/*List<Car> cars = new ArrayList<Car>();
		
		Car car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);
		
		car = new Car(session);
		car.setSpz("ZA 921 FC");
		car.setIsDefaultCar(true);
		car.setTachometerState(58960);
		car.setFuelType(eFuelType.NATURAL95);
		car.setNote("Moje vlastne auto");
		car.setTankSize(40);
		cars.add(car);		
		
		car = new Car(session);
		car.setSpz("CA 865 BR");
		car.setTachometerState(53856.56);
		car.setFuelType(eFuelType.NATURAL98);
		car.setNote("moje stare auto");
		car.setTankSize(40);
		cars.add(car);*/
		
		List<Car> cars = CarContract.getInstance().GetAllCars(this.getActivity());
		carsAdapter = new CarsAdapter(view.getContext(), cars);
		listView.setAdapter(carsAdapter);		
		
		SwipeDismissListViewTouchListener touchListener =
                new SwipeDismissListViewTouchListener(
                        listView,
                        new SwipeDismissListViewTouchListener.OnDismissCallback() {
                            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                for (int position : reverseSortedPositions) {
                                	Car car = carsAdapter.getItem(position);
                                	//CarContract.getInstance().DeleteCar(car, listView.getContext());
                                	carsAdapter.remove(car);
                                	
                                	Toast toast = Toast.makeText(listView.getContext(), R.string.zaznam_zmazany, Toast.LENGTH_LONG);
                                	toast.show();
                                }
                                carsAdapter.notifyDataSetChanged();
                            }
                        });
        listView.setOnTouchListener(touchListener);
        // Setting this scroll listener is required to ensure that during ListView scrolling,
        // we don't look for swipes.
        listView.setOnScrollListener(touchListener.makeScrollListener());        
	}
}
